package com.gisocn.onemap.mvp.presenter;

import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.Log;
import com.esri.android.map.Layer;
import com.esri.android.map.ags.ArcGISDynamicMapServiceLayer;
import com.esri.android.map.ags.ArcGISLayerInfo;
import com.esri.core.geometry.Geometry;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.MapGeometry;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.map.FeatureSet;
import com.esri.core.map.Field;
import com.esri.core.map.Graphic;
import com.esri.core.tasks.SpatialRelationship;
import com.esri.core.tasks.ags.query.Query;
import com.esri.core.tasks.ags.query.QueryTask;
import com.gisocn.onemap.base.Constant;
import com.gisocn.onemap.model.QueryStatisticsResultBean;
import com.gisocn.onemap.model.QueryXML;
import com.gisocn.onemap.mvp.MvpBasePresenter;
import com.gisocn.onemap.mvp.contact.QueryStatisticsContact;
import com.gisocn.onemap.utils.ExecutorsManager;
import com.gisocn.onemap.utils.LogUtils;
import com.gisocn.onemap.utils.PullHelper;
import com.gisocn.onemap.utils.ToastUtil;
import com.gisocn.onemap.utils.Utils;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import jsqlite.Callback;
import jsqlite.Database;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.util.MinimalPrettyPrinter;
import org.xmlpull.v1.XmlPullParserException;

/* loaded from: classes2.dex */
public class QueryStatisticsPresenter extends MvpBasePresenter<QueryStatisticsContact.View> implements QueryStatisticsContact.Presenter {
    private String[] coldataArray;
    private JsonObject landInfoObject;
    private List<ArcGISLayerInfo> layerInfoList = new ArrayList();
    private int asyncTaskIndex = 0;
    private List<QueryStatisticsResultBean> queryStatisticsResultList = new ArrayList();
    private List<AsyncQueryTask> asyncQueryTaskList = new ArrayList();

    static /* synthetic */ int access$208(QueryStatisticsPresenter queryStatisticsPresenter) {
        int i = queryStatisticsPresenter.asyncTaskIndex;
        queryStatisticsPresenter.asyncTaskIndex = i + 1;
        return i;
    }

    @Override // com.gisocn.onemap.mvp.contact.QueryStatisticsContact.Presenter
    public void sqlQuery(final String tableName, final String str2, final String str3, SpatialReference spatialReference, String str4) {
        ExecutorsManager.getInstance().execute(new Runnable() { // from class: com.gisocn.onemap.mvp.presenter.QueryStatisticsPresenter.1
            @Override // java.lang.Runnable
            public void run() {
                File file;
                QueryStatisticsPresenter.this.coldataArray = null;
                if (QueryStatisticsPresenter.this.queryStatisticsResultList.size() > 0) {
                    QueryStatisticsPresenter.this.queryStatisticsResultList.clear();
                }
                try {
                    String str5 = Constant.SAVE_PATH_SHP + tableName + ".db";
                    String file2 = new File(str5).toString();
                    Class.forName("jsqlite.JDBCDriver").newInstance();
                    Database database = new Database();
                    LogUtils.i("zxhhh56--> " + file2 + " , " + file.exists() + " , " + str5);
                    database.open(file2, 1);
                    Callback callback = new Callback() { // from class: com.gisocn.onemap.mvp.presenter.QueryStatisticsPresenter.1.1
                        @Override // jsqlite.Callback
                        public void types(String[] strArr) {
                        }

                        @Override // jsqlite.Callback
                        public void columns(String[] strArr) {
                            QueryStatisticsPresenter.this.coldataArray = strArr;
                        }

                        @Override // jsqlite.Callback
                        public boolean newrow(String[] strArr) {
                            String pOLYGONWktToJson;
                            Geometry geometry;
                            QueryStatisticsResultBean queryStatisticsResultBean = new QueryStatisticsResultBean();
                            JsonObject jsonObject = new JsonObject();
                            for (int i = 0; i < strArr.length; i++) {
                                if (!QueryStatisticsPresenter.this.coldataArray[i].equals("AsText(Geometry)")) {
                                    jsonObject.addProperty(QueryStatisticsPresenter.this.coldataArray[i], strArr[i]);
                                } else if (strArr[i] != null) {
                                    Log.e("rowdata", ":" + strArr[i]);
                                    if (Constant.FILEPROVIDER_AUTHORITY.equals("com.gisocn.onemap.yt")) {
                                        pOLYGONWktToJson = Utils.getMULTIPOLYGONWktToJson(strArr[i], Constant.WKID);
                                    } else if (strArr[i].contains("), (")) {
                                        pOLYGONWktToJson = Utils.getMULTIPOLYGONWktToJson(strArr[i], Constant.WKID);
                                    } else if (strArr[i].contains("MULTIPOLYGON")) {
                                        LogUtils.e(" Constant.WKID__________" + Constant.WKID);
                                        pOLYGONWktToJson = Utils.getMULTIPOLYGONWktToJson(strArr[i], Constant.WKID);
                                    } else if (strArr[i].contains("LINESTRING")) {
                                        if (strArr[i].contains("MULTILINESTRING")) {
                                            pOLYGONWktToJson = Utils.getMULTILINESTRINGWktToJson(strArr[i], Constant.WKID);
                                        } else {
                                            pOLYGONWktToJson = Utils.getLINESTRINGWktToJson(strArr[i], Constant.WKID);
                                        }
                                    } else {
                                        Log.e("POLYGO___", "" + strArr[i]);
                                        pOLYGONWktToJson = Utils.getPOLYGONWktToJson(strArr[i], Constant.WKID);
                                    }
                                    JsonParser jsonParser = null;
                                    try {
                                        jsonParser = new JsonFactory().createJsonParser(pOLYGONWktToJson);
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                    MapGeometry jsonToGeometry = GeometryEngine.jsonToGeometry(jsonParser);
                                    if (jsonToGeometry != null && (geometry = jsonToGeometry.getGeometry()) != null) {
                                        queryStatisticsResultBean.setGeometry(geometry);
                                    }
                                }
                            }
                            for (String str6 : jsonObject.keySet()) {
                                JsonElement jsonElement = jsonObject.get(str6);
                                if (jsonElement != null) {
                                    String asString = jsonElement.getAsString();
                                    if (str6.equals("PK_UID")) {
                                        queryStatisticsResultBean.setPkUid(asString);
                                    }
                                    if (str6.equals(str3)) {
                                        queryStatisticsResultBean.setDisplayFieldValue(asString);
                                    }
                                }
                            }
                            queryStatisticsResultBean.setTableName(tableName);
                            QueryStatisticsPresenter.this.queryStatisticsResultList.add(queryStatisticsResultBean);
                            return false;
                        }
                    };
                    String query = "SElECT " + ("pk_uid," + str3 + ",AsText(Geometry)") + " from " + tableName + " where " + str3 + " like '%" + str2 + "%'";
                    LogUtils.i("zxhhh522--> " + tableName + " , " + str5 + " , " + query);
                    database.exec(query, callback);
                    database.close();
                    Log.e("query", "查询结束");
                    LogUtils.i("zxhhh524--> " + QueryStatisticsPresenter.this.queryStatisticsResultList.size() + " , " + query + " , " + str5);
                    QueryStatisticsPresenter.this.getView().sqlQuerySuccess(QueryStatisticsPresenter.this.queryStatisticsResultList, query, str5);
                } catch (ClassNotFoundException e) {
                    QueryStatisticsPresenter.this.getView().sqlQueryFail("表:" + tableName + MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR + e.getMessage());
                    e.printStackTrace();
                } catch (IllegalAccessException e2) {
                    QueryStatisticsPresenter.this.getView().sqlQueryFail("表:" + tableName + MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR + e2.getMessage());
                    e2.printStackTrace();
                } catch (InstantiationException e3) {
                    QueryStatisticsPresenter.this.getView().sqlQueryFail("表:" + tableName + MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR + e3.getMessage());
                    e3.printStackTrace();
                } catch (Exception e4) {
                    e4.printStackTrace();
                    LogUtils.e("e.getMessage():" + tableName + ": " + e4.getMessage());
                    if (TextUtils.isEmpty(e4.getMessage()) || e4.getMessage().contains("Size")) {
                        return;
                    }
                    QueryStatisticsPresenter.this.getView().sqlQueryFail("表:" + tableName + MinimalPrettyPrinter.DEFAULT_ROOT_VALUE_SEPARATOR + e4.getMessage());
                }
            }
        });
    }

    @Override // com.gisocn.onemap.mvp.contact.QueryStatisticsContact.Presenter
    public void sqlHttpQuery(Layer layer, String str, String str2, String str3, String str4) {
        if (this.asyncQueryTaskList.size() > 0) {
            this.asyncQueryTaskList.clear();
        }
        if (this.queryStatisticsResultList.size() > 0) {
            this.queryStatisticsResultList.clear();
        }
        this.asyncTaskIndex = 0;
        if (layer != null) {
            if (layer instanceof ArcGISDynamicMapServiceLayer) {
                ArcGISDynamicMapServiceLayer arcGISDynamicMapServiceLayer = (ArcGISDynamicMapServiceLayer) layer;
                arcGISDynamicMapServiceLayer.retrieveLegendInfo();
                ArcGISLayerInfo[] layers = arcGISDynamicMapServiceLayer.getLayers();
                if (layers != null) {
                    if (this.layerInfoList.size() > 0) {
                        this.layerInfoList.clear();
                    }
                    getLayerInfo(layers, str);
                }
            }
            if (this.layerInfoList.size() > 0) {
                for (ArcGISLayerInfo arcGISLayerInfo : this.layerInfoList) {
                    Log.i("zxhhh", "zxhhh5007--> " + arcGISLayerInfo.getName() + " , " + arcGISLayerInfo.isVisible() + " , " + arcGISLayerInfo.getId() + " , " + layer.getUrl() + " , " + layer.getQueryUrl(arcGISLayerInfo.getId()));
                    if (arcGISLayerInfo.isVisible()) {
                        String queryUrl = layer.getQueryUrl(arcGISLayerInfo.getId());
                        if (!TextUtils.isEmpty(queryUrl)) {
                            this.asyncQueryTaskList.add(new AsyncQueryTask(queryUrl, str4, arcGISLayerInfo.getName(), str2, str3));
                        }
                    }
                }
            }
        }
        LogUtils.i("zxhhh271--> " + this.asyncQueryTaskList.size());
        if (this.asyncQueryTaskList.size() > 0) {
            for (AsyncQueryTask asyncQueryTask : this.asyncQueryTaskList) {
                asyncQueryTask.execute(new Float[0]);
            }
        }
    }

    /* loaded from: classes2.dex */
    private class AsyncQueryTask extends AsyncTask<Float, Void, FeatureSet> {
        String groupName;
        String keyWords;
        String name;
        String selectFieldName;
        String url;

        AsyncQueryTask(String str, String str2, String str3, String str4, String str5) {
            this.url = str;
            this.name = str3;
            this.groupName = str2;
            this.keyWords = str4;
            this.selectFieldName = str5;
        }

        /* JADX INFO: Access modifiers changed from: protected */
        @Override // android.os.AsyncTask
        public FeatureSet doInBackground(Float... fArr) {
            LogUtils.i("zxhhh272--> " + this.url + " , " + this.selectFieldName + " , " + this.keyWords);
            if (TextUtils.isEmpty(this.url)) {
                ToastUtil.show("配置有误，查询统计字段未正确设置");
                return null;
            } else if (TextUtils.isEmpty(this.selectFieldName)) {
                ToastUtil.show("配置有误，查询统计字段未正确设置");
                return null;
            } else {
                ArrayList arrayList = new ArrayList();
                if (!TextUtils.isEmpty(this.selectFieldName) && !arrayList.contains(this.selectFieldName)) {
                    arrayList.add(this.selectFieldName);
                }
                String str = this.selectFieldName + " like '%" + this.keyWords + "%'";
                LogUtils.i("zxhhh273--> " + str);
                Query query = new Query();
                query.setWhere(str);
                query.setReturnGeometry(true);
                query.setOutFields(new String[]{"*"});
                query.setSpatialRelationship(SpatialRelationship.INTERSECTS);
                try {
                    return new QueryTask(this.url).execute(query);
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            }
        }

        /* JADX INFO: Access modifiers changed from: protected */
        @Override // android.os.AsyncTask
        public void onPostExecute(FeatureSet featureSet) {
            Graphic[] graphics;
            Geometry geometry;
            Object attributeValue;
            QueryStatisticsPresenter.access$208(QueryStatisticsPresenter.this);
            if (featureSet != null && (graphics = featureSet.getGraphics()) != null && graphics.length > 0) {
                for (Graphic graphic : graphics) {
                    if (graphic != null && (geometry = graphic.getGeometry()) != null) {
                        QueryStatisticsResultBean queryStatisticsResultBean = new QueryStatisticsResultBean();
                        queryStatisticsResultBean.setGeometry(geometry);
                        List<Field> fields = featureSet.getFields();
                        if (fields != null && fields.size() > 0) {
                            ArrayList arrayList = new ArrayList();
                            for (Field field : fields) {
                                String name = field.getName();
                                if (!TextUtils.isEmpty(name) && (attributeValue = graphic.getAttributeValue(field.getName())) != null) {
                                    String replaceAll = attributeValue.toString().replaceAll("\\s+", "");
                                    if (!TextUtils.isEmpty(replaceAll)) {
                                        QueryStatisticsResultBean.FieldDataBean fieldDataBean = new QueryStatisticsResultBean.FieldDataBean();
                                        fieldDataBean.setFieldName(name);
                                        fieldDataBean.setAliasName(field.getAlias());
                                        fieldDataBean.setFieldValue(replaceAll);
                                        arrayList.add(fieldDataBean);
                                        if (!TextUtils.isEmpty(this.selectFieldName) && name.equals(this.selectFieldName)) {
                                            queryStatisticsResultBean.setDisplayFieldValue(replaceAll);
                                        }
                                    }
                                }
                            }
                            queryStatisticsResultBean.setFieldDataList(arrayList);
                        }
                        QueryStatisticsPresenter.this.queryStatisticsResultList.add(queryStatisticsResultBean);
                    }
                }
            }
            if (QueryStatisticsPresenter.this.asyncTaskIndex >= QueryStatisticsPresenter.this.asyncQueryTaskList.size()) {
                QueryStatisticsPresenter.this.getView().sqlQuerySuccess(QueryStatisticsPresenter.this.queryStatisticsResultList, "", "");
            }
        }
    }

    private void getLayerInfo(ArcGISLayerInfo[] arcGISLayerInfoArr, String str) {
        for (ArcGISLayerInfo arcGISLayerInfo : arcGISLayerInfoArr) {
            ArcGISLayerInfo[] layers = arcGISLayerInfo.getLayers();
            if (layers != null && layers.length > 0) {
                getLayerInfo(layers, str);
            } else {
                LogUtils.i("zxhhh223--> " + arcGISLayerInfo.getId() + " , " + arcGISLayerInfo.isVisible() + " , " + str);
                if (arcGISLayerInfo.isVisible() && str.contains(String.valueOf(arcGISLayerInfo.getId()))) {
                    this.layerInfoList.add(arcGISLayerInfo);
                }
            }
        }
    }

    @Override // com.gisocn.onemap.mvp.contact.QueryStatisticsContact.Presenter
    public void getXmlInfo(String str, int i, String str2) {
        File file = new File(Constant.SAVE_PATH_SHP + str + ".xml");
        if (file.exists()) {
            try {
                FileInputStream fileInputStream = new FileInputStream(file);
                try {
                    try {
                        getView().getXmlInfoSuccess(PullHelper.getQueryXML(fileInputStream), str2, str);
                        try {
                            fileInputStream.close();
                            return;
                        } catch (IOException e) {
                            e = e;
                            e.printStackTrace();
                            return;
                        }
                    } catch (XmlPullParserException e2) {
                        e2.printStackTrace();
                        try {
                            fileInputStream.close();
                            return;
                        } catch (IOException e3) {
                            e = e3;
                            e.printStackTrace();
                            return;
                        }
                    }
                } catch (IOException e4) {
                    e4.printStackTrace();
                    try {
                        fileInputStream.close();
                        return;
                    } catch (IOException e5) {
                        e = e5;
                        e.printStackTrace();
                        return;
                    }
                }
            } catch (FileNotFoundException e6) {
                e6.printStackTrace();
                return;
            }
        }
        getView().getXmlInfoFail("没有相关配置文件");
    }

    @Override // com.gisocn.onemap.mvp.contact.QueryStatisticsContact.Presenter
    public void sqlQueryLandInfo(final QueryXML queryXML, final String str, final String str2) {
        ExecutorsManager.getInstance().execute(new Runnable() { // from class: com.gisocn.onemap.mvp.presenter.QueryStatisticsPresenter.2
            @Override // java.lang.Runnable
            public void run() {
                QueryStatisticsPresenter.this.coldataArray = null;
                try {
                    String file = new File(Constant.SAVE_PATH_SHP + str + ".db").toString();
                    Class.forName("jsqlite.JDBCDriver").newInstance();
                    Database database = new Database();
                    database.open(file, 1);
                    Callback callback = new Callback() { // from class: com.gisocn.onemap.mvp.presenter.QueryStatisticsPresenter.2.1
                        @Override // jsqlite.Callback
                        public void types(String[] strArr) {
                        }

                        @Override // jsqlite.Callback
                        public void columns(String[] strArr) {
                            QueryStatisticsPresenter.this.coldataArray = strArr;
                        }

                        @Override // jsqlite.Callback
                        public boolean newrow(String[] strArr) {
                            QueryStatisticsPresenter.this.landInfoObject = new JsonObject();
                            for (int i = 0; i < strArr.length; i++) {
                                QueryStatisticsPresenter.this.landInfoObject.addProperty(QueryStatisticsPresenter.this.coldataArray[i], strArr[i]);
                            }
                            return false;
                        }
                    };
                    database.exec("SElECT * from " + str + " where pk_uid=" + str2, callback);
                    database.close();
                    QueryStatisticsPresenter.this.getView().sqlQueryLandInfoSuccess(QueryStatisticsPresenter.this.landInfoObject, queryXML);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e2) {
                    e2.printStackTrace();
                } catch (InstantiationException e3) {
                    e3.printStackTrace();
                } catch (Exception e4) {
                    e4.printStackTrace();
                }
            }
        });
    }
}
